package com.example.shop.service.impl;

import com.example.shop.mapper.UserMapper;
import com.example.shop.model.User;
import com.example.shop.service.UserService;
import com.example.shop.utils.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public User login(User user) {
        return userMapper.selectOne(user);
    }

    @Override
    public int register(User user) {
        return userMapper.insert(user);
    }

    @Override
    public int updateInfo(User user) {
        return userMapper.updateInfo(user);
    }

    @Override
    public int setUserHeadPic(User user) {
        return userMapper.setUserHeadPic(user);
    }

    @Override
    public PageBean<User> getUsers(Integer pageNo, Integer pageSize, String search) {
        PageBean<User> pageBean = new PageBean<>();
        pageBean.setPageSize(pageSize);
        pageBean.setDataCount(userMapper.getDataCount());
        pageBean.setPageNo(pageNo);
        if ("".equals(search) || search.length() == 0) {
            search = null;
        }
        List<User> users = userMapper.getUsers((pageNo - 1) * pageSize, pageSize, search);
        pageBean.setDataList(users);
        return pageBean;
    }

    @Override
    public int deleteUser(Integer id) {
        return userMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insertUser(User user) {
        return userMapper.insert(user);
    }

    @Override
    public int updateUser(User user) {
        return userMapper.updateInfo(user);
    }
}
